package us.mooresr.aircraftscheduling.client.ui;

import us.mooresr.aircraftscheduling.client.events.UserChangedEvent;
import us.mooresr.aircraftscheduling.client.events.UserCreatedEvent;
import us.mooresr.aircraftscheduling.shared.action.CreateUser;
import us.mooresr.aircraftscheduling.shared.action.CreateUserAction;
import us.mooresr.aircraftscheduling.shared.action.CreateUserResult;
import us.mooresr.aircraftscheduling.shared.action.UpdateUserAction;
import us.mooresr.aircraftscheduling.shared.action.UpdateUserResult;
import us.mooresr.aircraftscheduling.shared.model.User;
import us.mooresr.aircraftscheduling.shared.model.UserDetail;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.PopupView;
import com.gwtplatform.mvp.client.PresenterWidget;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class UserDetailPresenter extends
		PresenterWidget<UserDetailPresenter.MyView> 
		implements UserDetailUiHandlers {

	// These are functions that this Presenter (UserDetail) can tell its View to do
	public interface MyView extends PopupView, HasUiHandlers<UserDetailUiHandlers> {

		void clear();
		void preLoad(User user, UserDetail userDetail);

		
	}
	
	
	
	private final DispatchAsync dispatcher;
	private final PlaceManager placeManager;
	private final EventBus eventBus;

	@Inject
	public UserDetailPresenter(final EventBus eventBus, final MyView view,
			PlaceManager placeManager, DispatchAsync dispatcher) {
		super(eventBus, view);
		
		this.eventBus = eventBus;
		this.dispatcher = dispatcher;
		this.placeManager = placeManager;
		
		getView().setUiHandlers(this);
		
	}

	public void clear() {
		getView().clear();
		
	}

	@Override
	public void createUser(User user, UserDetail userDetail) {
		CreateUserAction action = new CreateUserAction(user, userDetail);
		dispatcher.execute(action, new AsyncCallback<CreateUserResult>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Got failure: " + caught.getMessage());
				
			}

			@Override
			public void onSuccess(CreateUserResult result) {
				
				
				UserCreatedEvent.fire(getEventBus(), result.getUserOut());
			}
			
		});

		
	}

	public void preLoad(User user, UserDetail userDetail) {
		
		getView().preLoad(user, userDetail);
	}

	@Override
	public void updateUser(User user, UserDetail userDetail) {
		UpdateUserAction action = new UpdateUserAction(user, userDetail);
		dispatcher.execute(action, new AsyncCallback<UpdateUserResult>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Got failure: " + caught.getMessage());
				
			}

			@Override
			public void onSuccess(UpdateUserResult result) {
				
				
				UserChangedEvent.fire(getEventBus(), result.getUserOut());
			}
			
		});
		
	}
	

}
